﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Registro.Compras.Api.Models;
using Registro.Compras.Data.Business;
using System.Threading.Tasks;
using System.Web.Http.Description;
using Registro.Compras.Data.Entities;
using System.Web.Http.Cors;

namespace Registro.Compras.Api.Controllers
{
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    public class ProveedorController : ApiController
    {
        // GET api/<controller>
        public IEnumerable<ProveedorModel> Get()
        {
            return ProveedorBusiness.GetListByFilter("").Select(t => new ProveedorModel
            {
                Id = t.Id,
                Tabla2Id = t.Tabla2Id,
                Tabla2Model = new Tabla2Model
                {
                    Id = t.Tabla2.Id,
                    Descripcion = t.Tabla2.Descripcion,
                },
                Numero = t.Numero,
                RazonSocial = t.RazonSocial,
            });
        }

        // GET api/<controller>/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<controller>
        public ProveedorModel Post([FromBody]ProveedorModel m)
        {
            Proveedor t = ProveedorBusiness.Create(new Proveedor
            {
                Tabla2Id = m.Tabla2Id,
                Numero = m.Numero,
                RazonSocial = m.RazonSocial,
            });
            return new ProveedorModel
            {
                Id = t.Id,
                Tabla2Id = t.Tabla2Id,
                Tabla2Model = new Tabla2Model
                {
                    Id = t.Tabla2.Id,
                    Descripcion = t.Tabla2.Descripcion,
                },
                Numero = t.Numero,
                RazonSocial = t.RazonSocial,
            };
        }

        // PUT api/<controller>/5
        public ProveedorModel Put(int id, [FromBody]ProveedorModel m)
        {
            Proveedor t = ProveedorBusiness.Update(new Proveedor
            {
                Id = id,
                Tabla2Id = m.Tabla2Id,
                Numero = m.Numero,
                RazonSocial = m.RazonSocial,
            });

            return new ProveedorModel
            {
                Id = t.Id,
                Tabla2Id = t.Tabla2Id,
                Tabla2Model = new Tabla2Model
                {
                    Id = t.Tabla2.Id,
                    Descripcion = t.Tabla2.Descripcion,
                },
                Numero = t.Numero,
                RazonSocial = t.RazonSocial,
            };

        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
            ProveedorBusiness.Delete(id);
        }
    }
}